﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data;
using System.Data.Common;
using System.Text;

public partial class MRP_DeliveryBill : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        //if (Request.Params["type"] != null)
        //{
        //    string xmlFileName = string.Format(@"XML/MRP_Product_Export_Delivery_{0}.xml", Request.Params["type"].ToString());
        //    this.UIServer1.XMLfile = xmlFileName;
        //    UIServer1.DBAllowUpdateDetails = true;

        //}

    }
    protected void UIServer1_CmdFieldFire(object sender, Evolutility.UIServer.CmdFieldEventArgs e)
    {
        
    }

    protected void UIServer1_DBChange(object sender, Evolutility.UIServer.DatabaseEventArgs e)
    {
        //int id;
        //if (Request.Params["type"] != null)
        //{
        //    if (Request.Params["type"].ToString().ToLower().Equals("signoff"))
        //    {
        //        if (e.Details != null)
        //        {

        //            Database db = DatabaseFactory.CreateDatabase("MRPSqlServer");
        //            foreach (Evolutility.UIServer.DatabaseDetailArgs item in e.Details)
        //            {
        //                foreach (int id in item.DetailIDs)
        //                {
        //                    string sqlCommand = "MRP_CheckOrderStatus";
        //                    DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand);
        //                    db.AddInParameter(dbCommand, "ItemID", DbType.Int32, id);
        //                    MRPUtility.WriteGeneralLog("试图更新客户订单的状态！");
        //                    db.ExecuteNonQuery(dbCommand);
        //                }
        //            }
        //        }
        //    }
        //}
    }
    protected void UIServer1_CmdFieldFire1(object sender, Evolutility.UIServer.CmdFieldEventArgs e)
    {
        int id = Int32.Parse(e.PK);
        int ExportTotal = 0,OrderQty=0,SentQty=0;
        string errorMsg = string.Empty;
        StringBuilder sbSQL = new StringBuilder(),sbUpdateSQL=new StringBuilder();
        

        if (e.CommandText.Equals("发布"))
        {
            Database db = DatabaseFactory.CreateDatabase("MRPSqlServer");
            string sSQL = String.Format("select OrderID,ProductID,SendOutQuantity from [MRP_DeliveryItem] where billID={0}", id);
            using (IDataReader reader1 = db.ExecuteReader(CommandType.Text, sSQL))
            {
                while (reader1.Read())
                {



                    //检查数据
                    sbSQL = null;
                    sbSQL = new StringBuilder();
                    sbSQL.Append("SELECT Sum(View_ProductExportTotalByOrderAndMaterial.TotalQuantity),Sum(MRP_DeliveryItem.SendOutQuantity), SUM(MRP_OrderDetail.Quantity),MRP_DeliveryItem.ProductID, MRP_DeliveryItem.OrderID ");
                    sbSQL.Append(" FROM MRP_OrderDetail INNER JOIN MRP_DeliveryItem ON MRP_OrderDetail.OrderID = MRP_DeliveryItem.OrderID AND MRP_OrderDetail.ProductID = MRP_DeliveryItem.ProductID ");
                    sbSQL.Append(" Left JOIN View_ProductExportTotalByOrderAndMaterial ON MRP_OrderDetail.OrderID = View_ProductExportTotalByOrderAndMaterial.OrderID AND MRP_OrderDetail.ProductID = View_ProductExportTotalByOrderAndMaterial.MaterialID");
                    sbSQL.AppendFormat(" Where MRP_DeliveryItem.OrderID={0} and MRP_DeliveryItem.ProductID={1} Group By MRP_DeliveryItem.ProductID, MRP_DeliveryItem.OrderID;", reader1[0],reader1[1]);

                    using (IDataReader reader = db.ExecuteReader(CommandType.Text, sbSQL.ToString()))
                    {
                        if (reader.Read())
                        {
                            if (!String.IsNullOrEmpty(reader[0].ToString()))
                                ExportTotal = Int32.Parse(reader[0].ToString());
                            if (!String.IsNullOrEmpty(reader[1].ToString()))
                                SentQty = Int32.Parse(reader[1].ToString());
                            if (!String.IsNullOrEmpty(reader[2].ToString()))
                                OrderQty = Int32.Parse(reader[2].ToString());
                        }
                        else
                            SentQty = Int32.Parse(reader1[2].ToString());

                            if ((double)(OrderQty * 1.1) < (double)SentQty) //送货数量不应该超过订单的总数的110%。
                            {
                                errorMsg += string.Format("送货数量{0}(含本次{1})不应该超过订单的总数{2}的10%。\n", SentQty, reader1[2], OrderQty);
                            }
                            if (ExportTotal < SentQty)//送货数量应该少于当前出库数量。
                            {
                                errorMsg += string.Format("送货数量{0}(含本次{1})应该少于当前出库数量{1}。\n", SentQty, reader1[2], ExportTotal);
                            }

                        
                    }

                   
                }
            }
            if (string.IsNullOrEmpty(errorMsg))
            {
                sbUpdateSQL.AppendFormat("Update dbo.MRP_DeliveryBill set Status=2 where ID = {0};", id);
                sbUpdateSQL.AppendFormat("Update dbo.MRP_DeliveryItem set Status=2 where BillID = {0};", id);
                db.ExecuteNonQuery(CommandType.Text, sbUpdateSQL.ToString());
                this.MsgForPublish.Text = "";
            }
            else
            {
                errorMsg += "发布不成功，无法送货！请重新修改数据。";
                this.MsgForPublish.Text = errorMsg;
            }

        }
    }
}
